package com.atguigu.rabbitmq.three;

import com.atguigu.rabbitmq.util.RabbitMqUtils;
import com.atguigu.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * 消息手动应答时不丢失,放回对应中从新消费 消费者
 */
public class Work03 {

    public static final String task_queue_name = "ask_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C1等待消息处理时间较短");


        DeliverCallback deliverCallback = (consumerTag,message) -> {
            // 沉睡1s
            SleepUtils.sleep(1);
            System.out.println("接收到的消息："+new String(message.getBody(), "utf-8"));

            // 手动应答的代码
            /**
             * 1. 消息的标记 tag
             * 2. 是否批量应答 false 不批量应答 信道中的消息
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        // 设置成不公平分发
        //channel.basicQos(1); // 1不公平 0公平

        // 设置预取值 5
        channel.basicQos(2);
        // 从队列中接受消息，采用手动应答
        boolean autoAck = false;
        channel.basicConsume(task_queue_name,autoAck,deliverCallback,consumerTag -> {
            System.out.println("消费者取消消费接口回调逻辑");
        });



    }
}
